define(['amazeui', 'jquery', 'global/url', 'global/constants', 'flot', 'flot-time'],
  function(amazeui, jquery, url, constants) {
    return ['$scope', '$rootScope', 'UrlService', 'UtilService', 'AdminService', function($scope, $rootScope, UrlService, UtilService, AdminService) {
	   
	   	if (!$scope.accessToken) {
	   		window.location.href = constants.pages.login;
	   	}

	   	$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
			if (toState.name=='login') {
				return;
			}
			if (!$rootScope.accessToken) {
				event.preventDefault();
				$state.go("login", {from:fromState.name, w:'notLogin'});
			}
		});

	    var options = UtilService.getChartOptions();

        function _renderDAU () {
            var apiURL = url.getDAUURL();
	    	UrlService.doRequest(apiURL, 'GET').success(function(response, status) {
		  		if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
		  			var $ele = $('#site_statistics'), $loading = $('#site_statistics_loading'), $content = $('#site_statistics_content'), label = 'Daily New'; 
		  		    UtilService.renderChart($ele, $loading, $content, label, response.jsonResponse.response);
		  	    } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
		  	        AdminService.Admin.logout();
		  	        window.location.href = constants.pages.login;
		  	    }
            });
        }

        function _renderDNU () {
            var apiURL = url.getDNUURL();
	    	UrlService.doRequest(apiURL, 'GET').success(function(response, status) {
		  		if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
		  		    var $ele = $('#site_statistics_login'), $loading = $('#site_statistics_loading_login'), $content = $('#site_statistics_content_login'), label = 'Daily Login'; 
		  		    UtilService.renderChart($ele, $loading, $content, label, response.jsonResponse.response);
		  	    } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
		  	       	AdminService.Admin.logout();
		  	        window.location.href = constants.pages.login;
		  	    }
            });
        }

        _renderDAU();
        _renderDNU();
    }];
  }
)
